package org.dromara.mall.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.apache.dubbo.config.annotation.DubboReference;
import org.dromara.system.api.RemoteDictService;
import org.dromara.system.api.domain.vo.RemoteDictDataVo;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.mall.domain.vo.TerMallProductVo;
import org.dromara.mall.domain.bo.TerMallProductBo;
import org.dromara.mall.service.ITerMallProductService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 商品
 * 前端访问路由地址为:/mall/mallProduct
 *
 * @author xch
 * @date 2025-06-27
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/mallProduct")
public class TerMallProductController extends BaseController {
    @DubboReference
    private RemoteDictService remoteDictService;

    private final ITerMallProductService terMallProductService;

    /**
     * 查询商品列表
     */
    @SaCheckPermission("mall:mallProduct:list")
    @GetMapping("/list")
    public TableDataInfo<TerMallProductVo> list(TerMallProductBo bo, PageQuery pageQuery) {
        return terMallProductService.queryPageList(bo, pageQuery);
    }
    @GetMapping("/wechat/queryDict")
    public R<List<RemoteDictDataVo>> queryDict() {
        return R.ok(remoteDictService.selectDictDataByType("ter_category"));
    }

    @GetMapping("/wechat/query")
    public TableDataInfo<TerMallProductVo> query(TerMallProductBo bo, PageQuery pageQuery) {
        return terMallProductService.queryPageList(bo, pageQuery);
    }
    /**
     * 导出商品列表
     */
    @SaCheckPermission("mall:mallProduct:export")
    @Log(title = "商品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TerMallProductBo bo, HttpServletResponse response) {
        List<TerMallProductVo> list = terMallProductService.queryList(bo);
        ExcelUtil.exportExcel(list, "商品", TerMallProductVo.class, response);
    }

    /**
     * 获取商品详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("mall:mallProduct:query")
    @GetMapping("/{id}")
    public R<TerMallProductVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable("id") Long id) {
        return R.ok(terMallProductService.queryById(id));
    }

    /**
     * 新增商品
     */
    @SaCheckPermission("mall:mallProduct:add")
    @Log(title = "商品", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TerMallProductBo bo) {
        return toAjax(terMallProductService.insertByBo(bo));
    }

    /**
     * 修改商品
     */
    @SaCheckPermission("mall:mallProduct:edit")
    @Log(title = "商品", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TerMallProductBo bo) {
        return toAjax(terMallProductService.updateByBo(bo));
    }

    /**
     * 删除商品
     *
     * @param ids 主键串
     */
    @SaCheckPermission("mall:mallProduct:remove")
    @Log(title = "商品", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable("ids") Long[] ids) {
        return toAjax(terMallProductService.deleteWithValidByIds(List.of(ids), true));
    }
}
